package com.atguigu.tingshu.search.factory;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * <a href="https://www.shallowrecall.top/">Recall`Blog</a></br>
 * <a href="https://github.com/ShallowRecall">GitHub</a>
 *
 * @author Recall
 * @createTime 2025/03/27 10:47
 **/
public class ScheduleTaskThreadPoolFactory {

    static ScheduledExecutorService scheduledExecutorService = null;


    // 加载ScheduleTaskThreadPoolFactory类的时候 提前将定时任务的线程池对象创建出来
    static {
        scheduledExecutorService = Executors.newScheduledThreadPool(2);
    }

    /**
     * 定义一个实例对象（单例设计模式）饿汉式
     * 懒汉式
     * 饿汉式
     * double check
     * 枚举
     */

    private static ScheduleTaskThreadPoolFactory INSTANCE = new ScheduleTaskThreadPoolFactory();

    /**
     * 提供一个方法 获取实例对象
     */
    public static ScheduleTaskThreadPoolFactory getInstance(){
        return INSTANCE;
    }

    /**
     * 私有化构造器
     */
    private ScheduleTaskThreadPoolFactory(){

    }


    /**
     * 提供一个方法 接外部提交的任务
     */
    public void execute(Runnable runnable, Long ttl, TimeUnit timeUnit){
        scheduledExecutorService.schedule(runnable, ttl, timeUnit);
    }

    /**
     * 提供一个方法 计算时间差
     */
    public Long diffTime(Long currentTime){
        LocalDate localDate = LocalDate.now().plusDays(7);
        LocalDateTime localDateTime = LocalDateTime.of(localDate, LocalTime.of(2, 0, 0));
        long time = localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
        long diffTime =   currentTime - time;
        return Math.abs(diffTime);
    }

}
